package com.lms.busi.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lms.busi.common.service.ISysCommonCodeService;
import com.lms.common.entity.SysCommonCode;
import com.lms.common.pure.entity.TreeNode;
import com.lms.common.pure.query.QueryParams;
import com.lms.common.pure.res.CodeRes;
import com.lms.common.web.controller.BaseController;
import com.lms.common.web.utils.CheckEntityResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysCommonCode")
@Tag(name="公共码值表相关请求")
public class SysCommonCodeController extends BaseController<ISysCommonCodeService, SysCommonCode> {
    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @GetMapping("/toggleStatus/{id}")
    public void toggleStatus(@PathVariable("id") String id) {
        this.baseService.toggleStatus(id);
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @Override
    @Operation(summary = "通用表格查询方法")
    @PostMapping("/query")
    public IPage<?> query(@Parameter(description = "查询参数") @RequestBody QueryParams<Map<String, Object>> queryParams) {
        return super.query(queryParams);
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @Override
    @Operation(summary = "根据主键id获取相应实体或View信息")
    @GetMapping("/get/{id}")
    public Object get(@PathVariable("id") @Parameter(description = "主键id") Serializable id) {
        return super.get(id);
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @Override
    @Operation(summary = "根据条件获取相应实体或View信息")
    @PostMapping("/getByFilter")
    public Object getByFilter(@Parameter(description = "查询参数") @RequestBody Map<String, Object> queryParams) {
        return super.getByFilter(queryParams);
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @Override
    @Operation(summary = "通用保存方法")
    @PostMapping("/save")
    public SysCommonCode save(@Parameter(description = "保存的实体类信息") @RequestBody Map<String, Object> entity) {
        return super.save(entity);
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @Override
    @Operation(summary = "保存前的校验方法")
    @PostMapping("/beforeSaveCheck")
    public CheckEntityResult beforeSaveCheck(@Parameter(description = "需要校验的保存对象") @RequestBody SysCommonCode entity) {
        return super.beforeSaveCheck(entity);
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @Override
    @Operation(summary = "根据主键id删除单条信息")
    @DeleteMapping("/delete/{id}")
    public void delete(@Parameter(description = "主键id") @PathVariable("id") Serializable id) {
        super.delete(id);
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMPANY_ADMIN')")
    @Override
    @Operation(summary = "根据主键id集合删除多条信息")
    @PostMapping("/deleteBatch")
    public void deleteBatch(@Parameter(description = "主键id集合") @RequestBody List<Serializable> ids) {
        super.deleteBatch(ids);
    }


    @Operation(summary = "根据大类代码获取代码集合")
    @GetMapping("/codeList/{categoryCode}")
    public List<CodeRes> codeList(@Parameter(description = "大类代码") @PathVariable("categoryCode") String categoryCode) {
        return this.baseService.getByCategory(categoryCode);
    }

    @Operation(summary = "根据大类代码获取代码集合")
    @PostMapping("/codeListByParent")
    public List<CodeRes> codeListByParent(@RequestBody SysCommonCode params) {
        return this.baseService.getByCategory(params.getCategoryCode(), params.getParentId());
    }

    @Operation(summary = "获取公共代码表的树结构")
    @GetMapping("/codeTree")
    public List<TreeNode> codeTree() {
        return this.baseService.codeTree();

    }

}
